package com.easy.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.easy.common.core.R;
import com.easy.common.core.domain.model.ESHouseInfo;
import com.easy.common.es.ESHouseService;
import com.easy.common.es.impl.ESHouseServiceImpl;
import com.easy.user.pojo.Membership;
import com.easy.user.service.IMembershipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 搜索引擎控制器
 *
 * @author RuoYu
 * @date 2022/12/25
 */
@RequestMapping("/es")
@RestController
@Api(tags = "搜索引擎控制器")
public class ESController {

    @Autowired
    private ESHouseService esHouseService;

    @Autowired
    private IMembershipService membershipService;

    @GetMapping()
    @ApiOperation(value = "搜索入口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "一页数量", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "value", value = "关键词", dataTypeClass = String.class),
    })
    public R get(int page, int pageSize, String value) {
        JSONObject jsonObject = esHouseService.conditionalPaginationQuery(page, pageSize, "title", value);
        return R.success(jsonObject);
    }

    @PostMapping()
    @ApiOperation(value = "添加文档")
    @ApiImplicitParam(name = "esHouseInfo", value = "房子简略信息", dataTypeClass = String.class, required = true)
    public R put(@RequestBody ESHouseInfo esHouseInfo) {
        // 1.获取会员信息
        Membership membership = membershipService.getById(esHouseInfo.getId());
        // 2.获取会员等级
        int membershipLevel = membership.getMembershipLevel();
        // 3.设置权重
        if (membershipLevel > 0) {
            // 3.12会员权重
            esHouseInfo.setW(String.valueOf(membershipLevel));
        } else {
            // 3.2非会员权重
            esHouseInfo.setW("0.0");
        }
        System.out.println(esHouseInfo);
        esHouseService.addDoc(esHouseInfo);
        return R.success();
    }

    //删除
    @DeleteMapping()
    @ApiOperation(value = "删除文档")
    @ApiImplicitParam(name = "id", value = "房子id", dataTypeClass = Long.class, required = true)
    public R delete(String id) {
        ESHouseInfo esHouseInfo = new ESHouseInfo();
        esHouseInfo.setId(id);
        esHouseService.deleteDoc(esHouseInfo);
        return R.success();
    }

    //修改
    @PutMapping()
    @ApiOperation(value = "修改文档")
    @ApiImplicitParam(name = "esHouseInfo", value = "房子简略信息", dataTypeClass = String.class, required = true)
    public R update(@RequestBody ESHouseInfo esHouseInfo) {
        System.out.println(esHouseInfo);
        esHouseService.updateDoc(esHouseInfo);
        return R.success();
    }

}
